package com.game.chinesechess.check;


import com.game.chinesechess.GameInfo;
import com.game.chinesechess.piece.chess;

public class rangeSense {
    public static boolean canSelect(chess[][] board, int x, int y, chess.Color turn) {
        if (x < 0 || x >= 10 || y < 0 || y >= 9)
            return false;
        if (board[x][y] == null)
            return false;
        return turn == board[x][y].color;
    }

    public static boolean canMove(chess[][] board, int b_x, int b_y, int e_x, int e_y, chess.Color turn) {
        if ((b_x < 0 || b_x >= 10 || b_y < 0 || b_y >= 9) && (e_x < 0 || e_x >= 10 || e_y < 0 || e_y >= 9))
            return false;
        if (board[e_x][e_y] != null) {
            return board[b_x][b_y].check(board, e_x, e_y)&&turn!=board[e_x][e_y].color;
        } else {
            return board[b_x][b_y].check(board, e_x, e_y) ;
        }
    }

    public static boolean isOver(GameInfo gameInfo){
        chess b_general=null,r_general=null;
        for(int i=0;i<gameInfo.board.length;i++)
            for(int j=0;j<gameInfo.board[0].length;j++){
                if((gameInfo.board[i][j]==null?"空":gameInfo.board[i][j].getName()).equals("帅"))
                    r_general=gameInfo.board[i][j];
                if((gameInfo.board[i][j]==null?"空":gameInfo.board[i][j].getName()).equals("将"))
                    b_general=gameInfo.board[i][j];
            }
        if(r_general!=null&&b_general!=null){
            if(r_general.point.y==b_general.point.y){
                for(int i=b_general.point.x+1;i<r_general.point.x;i++)
                    if(gameInfo.board[i][r_general.point.y]!=null)
                        return false;
                gameInfo.Winner=gameInfo.turn;
                return true;
            }else
                return false;
        }else{
            if(r_general==null){
                gameInfo.Winner= chess.Color.BlACK;
            }else {
                gameInfo.Winner= chess.Color.RED;
            }
            return true;
        }
    }
}